/**
 * 
 */
package com.heer.RD.projecthour.page;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.wicket.extensions.ajax.markup.html.tabs.AjaxTabbedPanel;
import org.apache.wicket.extensions.markup.html.tabs.AbstractTab;
import org.apache.wicket.extensions.markup.html.tabs.ITab;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.HiddenField;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.value.ValueMap;
import org.springframework.jdbc.core.JdbcTemplate;

import com.heer.RD.auth.HeerSession;
import com.heer.RD.projecthour.component.DeptChoice;
import com.heer.RD.projecthour.page.include.FooterPanel;
import com.heer.RD.projecthour.page.include.HeaderPanel;
import com.heer.RD.projecthour.page.include.NavPanel;
import com.heer.RD.projecthour.pojo.UserInfoDTO;
import com.heer.RD.util.UtilsMisc;
import com.heer.joss.persistence.IBaseDAO;

/**
 * @author ZhaoZehui
 *
 */
public class LoginPage extends WebPage {

	public LoginPage() {
		
		if(HeerSession.session().isSignIn()) {
			setResponsePage(MainPage.class);
		}
		
		add(new HiddenField("currentMenuId", new Model<String>(this.getPageClass().getName().replaceAll("\\.", "_")))
					.setMarkupId("currentMenuId")
					.setOutputMarkupId(true));
		
		add(new HeaderPanel("header", this));
		add(new NavPanel("nav", this));
		add(new FooterPanel("footer", this));

		
		List<ITab> tabs = new ArrayList<ITab>();
		tabs.add(new AbstractTab(new Model("登录")){
			public Panel getPanel(String panelId) {
				return new LoginPanel(panelId);
			}
		});

		tabs.add(new AbstractTab(new Model("注册")) {
			public Panel getPanel(String panelId) {
				return new RegPanel(panelId);
			}
		});
		
		add(new AjaxTabbedPanel("tabs", tabs));
		
	}
	@SpringBean
	private JdbcTemplate jdbcTemplate;
	
	@SpringBean(name="userDao")
	private IBaseDAO userDao;

	public IBaseDAO getUserDao() {
		return userDao;
	}

	public void setUserDao(IBaseDAO userDao) {
		this.userDao = userDao;
	}
	
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	private class LoginPanel extends Panel {

		public LoginPanel(String id) {
			super(id);
			
			final FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
			feedbackPanel.setVisible(false);
			
			Form loginForm = new Form("loginForm", new CompoundPropertyModel(new ValueMap())) {
				protected void onSubmit() {
					final ValueMap values = (ValueMap)getModelObject();

					if (StringUtils.isBlank((String)values.get("loginName"))) {
						error("登录名不能为空!");
						feedbackPanel.setVisible(true);
					}
					else if (StringUtils.isBlank((String)values.get("loginPass"))) {
						error("密码不能为空!");
						feedbackPanel.setVisible(true);
					}
					else {
						UserInfoDTO queryDto = new UserInfoDTO((String)values.get("loginName"), (String)values.get("loginPass"));
						queryDto.setIsUsed(0);
						List<UserInfoDTO> uList = userDao.query(queryDto, "");
						if(CollectionUtils.isEmpty(uList)) {
							error("登录名/密码不正确!");
							feedbackPanel.setVisible(true);
						}
						else {
							HeerSession session = (HeerSession)getSession();
							UserInfoDTO user = uList.get(0);
							session.setUser(user);
							setResponsePage(getApplication().getHomePage());
						}
					}
				}
			};
			
			TextField loginName = new TextField("loginName");
			loginForm.add(loginName);
			
			PasswordTextField pass = new PasswordTextField("loginPass"); 
			pass.setRequired(false);
			loginForm.add(pass);
			
			loginForm.add(feedbackPanel);
			add(loginForm);
		}
		
	}
	
	private class RegPanel extends Panel {

		public RegPanel(String id) {
			super(id);
			
			final FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
			feedbackPanel.setVisible(false);
			
			final DeptChoice depts = new DeptChoice("depts", getJdbcTemplate(), "root");
			depts.setOutputMarkupId(true);
			
			Form regForm = new Form("regForm", new CompoundPropertyModel(new ValueMap())) {
				protected void onSubmit() {
					final ValueMap values = (ValueMap)getModelObject();

					if (StringUtils.isBlank((String)values.get("loginName"))) {
						error("登录名不能为空!");
						feedbackPanel.setVisible(true);
					}
					else if (StringUtils.isBlank((String)values.get("loginPass"))) {
						error("密码不能为空!");
						feedbackPanel.setVisible(true);
					}
					else if (!values.get("loginPass").equals(values.get("reLoginPass"))) {
						error("两次密码输入不一致!");
						feedbackPanel.setVisible(true);
					}
					else if (StringUtils.isBlank((String)values.get("xm"))) {
						error("姓名不能为空!");
						feedbackPanel.setVisible(true);
					}
					else if (StringUtils.isEmpty(depts.getModelValue()) || "-1".equals(depts.getModelValue())) {
						error("部门不能为空!");
						feedbackPanel.setVisible(true);
					}
					else if (StringUtils.isBlank((String)values.get("mail")) || !UtilsMisc.checkEmail((String)values.get("mail"))) {
						error("邮箱不能为空且要符合格式!");
						feedbackPanel.setVisible(true);
					}
					else {
						
						List quList = getUserDao().queryByCriteria("user_name = '"+(String)values.get("loginName")+"'");
						if(!CollectionUtils.isEmpty(quList)) {
							error("登录名已存在!");
							feedbackPanel.setVisible(true);
						}
						else {
							UserInfoDTO newDto = new UserInfoDTO((String)values.get("loginName"), (String)values.get("loginPass"));
							newDto.setDeptId(depts.getModelValue());
							newDto.setName((String)values.get("xm"));
							newDto.setMail((String)values.get("mail"));
							newDto.setUserRole("1000");
							newDto.setIsUsed(0);
							newDto.setUpdateDate(UtilsMisc.getCurrTimeStr());
							getUserDao().create(newDto);
							
							info("添加成功，请登录。");
							values.clear();
							feedbackPanel.setVisible(true);
						}
						
					}
				}
			};
			
			TextField loginName = new TextField("loginName");
			regForm.add(loginName);
			
			PasswordTextField pass = new PasswordTextField("loginPass"); 
			pass.setRequired(false);
			regForm.add(pass);
			
			PasswordTextField repass = new PasswordTextField("reLoginPass"); 
			repass.setRequired(false);
			regForm.add(repass);
			
			TextField xm = new TextField("xm");
			regForm.add(xm);
			
			TextField mail = new TextField("mail");
			regForm.add(mail);
			
			regForm.add(depts);
			
			regForm.add(feedbackPanel);
			add(regForm);
		}
		
	}
}
